<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单唯一编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单唯一编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="客户" prop="clientId">
        <el-input v-model="queryParams.clientId" placeholder="请输入客户" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="收运人员" prop="collectorId">
        <el-input v-model="queryParams.collectorId" placeholder="请输入收运人员" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input v-model="queryParams.weight" placeholder="请输入重量" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="queryParams.unitPrice" placeholder="请输入单价" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="价值" prop="totalValue">
        <el-input v-model="queryParams.totalValue" placeholder="请输入价值" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" style="width: 200px" placeholder="请选择状态" clearable>
          <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['order:order:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['order:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <el-table border stripe v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="订单唯一编号"
        align="center"
        prop="orderNo"
        v-if="columns[1].visible"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="客户"
        align="center"
        prop="clientId"
        v-if="columns[2].visible"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="收运人员"
        align="center"
        prop="collectorId"
        v-if="columns[3].visible"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="重量"
        align="center"
        prop="weight"
        v-if="columns[4].visible"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="单价"
        align="center"
        prop="unitPrice"
        v-if="columns[5].visible"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="价值"
        align="center"
        prop="totalValue"
        v-if="columns[6].visible"
        :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" v-if="columns[7].visible">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="images" width="100" v-if="columns[8].visible">
        <template #default="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        width="180"
        v-if="columns[9].visible"
        :show-overflow-tooltip="true">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatedAt"
        width="180"
        v-if="columns[10].visible"
        :show-overflow-tooltip="true">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单地址"
        align="center"
        prop="address"
        v-if="columns[11].visible"
        :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:order:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改回收订单对话框 -->
    <el-dialog
      class="adding-order__container"
      :title="title"
      v-model="open"
      width="600px"
      append-to-body
      destroy-on-close>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="120px" label-position="top">
        <el-form-item label="订单唯一编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单唯一编号" />
        </el-form-item>
        <el-form-item label="客户" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="收运人员" prop="collectorId">
          <el-input v-model="form.collectorId" placeholder="请输入收运人员" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <image-upload v-model="form.images" />
        </el-form-item>
        <el-form-item label="订单地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入订单地址" />
        </el-form-item>
        <t-map
          @locationUpdate="handleLocationUpdate"
          :location="{
            address: form.address
          }" />
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from '@/api/order/order'
import TMap from '@/components/TMap/index.vue'

const { proxy } = getCurrentInstance()
const { order_status } = proxy.useDict('order_status')

const orderList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    clientId: null,
    collectorId: null,
    weight: null,
    unitPrice: null,
    status: null
  },
  rules: {
    orderNo: [{ required: true, message: '订单唯一编号不能为空', trigger: 'blur' }],
    weight: [{ required: true, message: '重量不能为空', trigger: 'blur' }],
    unitPrice: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    images: [{ required: true, message: '图片不能为空', trigger: 'blur' }]
  },
  //表格展示列
  columns: [
    { key: 0, label: '', visible: true },
    { key: 1, label: '订单唯一编号', visible: true },
    { key: 2, label: '客户', visible: true },
    { key: 3, label: '收运人员', visible: true },
    { key: 4, label: '重量', visible: true },
    { key: 5, label: '单价', visible: true },
    { key: 6, label: '价值', visible: true },
    { key: 7, label: '状态', visible: true },
    { key: 8, label: '图片', visible: true },
    { key: 9, label: '创建时间', visible: true },
    { key: 10, label: '更新时间', visible: true },
    { key: 11, label: '订单地址', visible: true }
  ]
})

const { queryParams, form, rules, columns } = toRefs(data)

/** 查询回收订单列表 */
function getList() {
  loading.value = true
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderNo: null,
    clientId: null,
    collectorId: null,
    weight: null,
    unitPrice: null,
    totalValue: null,
    status: null,
    images: null,
    createdAt: null,
    updatedAt: null,
    address: null
  }
  proxy.resetForm('orderRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加回收订单'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _orderId = row.orderId || ids.value
  getOrder(_orderId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改回收订单'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['orderRef'].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value
  proxy.$modal
    .confirm('是否确认删除回收订单编号为"' + _orderIds + '"的数据项？')
    .then(function () {
      return delOrder(_orderIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'order/order/export',
    {
      ...queryParams.value
    },
    `order_${new Date().getTime()}.xlsx`
  )
}
function handleLocationUpdate(location) {
  const { standard_address, recommend } = location.formatted_addresses
  form.value.address = `${standard_address} ${recommend}`
  form.value.latitude = location.location.lat
  form.value.longitude = location.location.lng
}

getList()
</script>
<style>
.adding-order__container .el-dialog__body {
  height: calc(80vh);
  overflow: auto;
}
</style>
